package com.sfddata.musicpillow.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfddata.musicpillow.common.utils.PageUtils;
import com.sfddata.musicpillow.common.utils.Query;
import com.sfddata.musicpillow.user.dao.UserInfoDao;
import com.sfddata.musicpillow.user.entity.EquipmentInfoEntity;
import com.sfddata.musicpillow.user.entity.UserEquipRelationEntity;
import com.sfddata.musicpillow.user.entity.UserInfoEntity;
import com.sfddata.musicpillow.user.exception.PasswordErrorException;
import com.sfddata.musicpillow.user.exception.UserNotExsitException;
import com.sfddata.musicpillow.user.exception.UserNotSetPasswordException;
import com.sfddata.musicpillow.user.service.EquipmentInfoService;
import com.sfddata.musicpillow.user.service.UserEquipRelationService;
import com.sfddata.musicpillow.user.service.UserInfoService;
import com.sfddata.musicpillow.user.vo.CheckPassWord;
import com.sfddata.musicpillow.user.vo.UserInfoEntityVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;


@Service("userInfoService")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfoEntity> implements UserInfoService {


    @Autowired
    private EquipmentInfoService equipmentInfoService;
    @Autowired
    private UserEquipRelationService userEquipRelationService;

    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserInfoEntity> page = this.page(
                new Query<UserInfoEntity>().getPage(params),
                new QueryWrapper<UserInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserInfoEntity getByAccount(String phone) {

        QueryWrapper<UserInfoEntity> qw = new QueryWrapper<>();
        qw.eq("user_account", phone);
        UserInfoEntity userInfoEntity = baseMapper.selectOne(qw);
        return userInfoEntity;
    }

    @Override
    public UserInfoEntity updateUserInfo(UserInfoEntityVo userInfoVo) {

        UserInfoEntity userInfoEntity = new UserInfoEntity();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        BeanUtils.copyProperties(userInfoVo, userInfoEntity);
        QueryWrapper<UserInfoEntity> qw = new QueryWrapper<>();
        qw.eq("user_account", userInfoVo.getUserAccount());
        UserInfoEntity one = this.getOne(qw);

        // 更新用户信息
        if (userInfoVo.getPassword() == null) {
            // 如果密码为空，那么就是更新其他数据
            this.update(userInfoEntity, qw);
        }else {
            // 密码不为空，就只是更新密码
            String password = userInfoVo.getPassword();
            String encodePassword = bCryptPasswordEncoder.encode(password);
            if (one == null){
                return null;
            }
            one.setPassword(encodePassword);
            this.update(one, qw);
        }

        UserInfoEntity userInfo = baseMapper.selectOne(qw);
        // 更新用户的设备信息
//        List<EquipmentInfoEntity> eqList = userInfoVo.getEqList();
//        UserEquipRelationEntity relationEntity = new UserEquipRelationEntity();
//        relationEntity.setUserId(userInfo.getId());
//        if (eqList != null){
//            equipmentInfoService.saveBatch(eqList);
//            for (EquipmentInfoEntity entity : eqList) {
//                equipmentInfoService.save(entity);
//                relationEntity.setEquipId(entity.getId());
//                userEquipRelationService.save(relationEntity);
//            }
//
//        }
        return userInfo;
    }

    @Override
    public UserInfoEntity insertIfDontExist(String phoneCheck) {

        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", phoneCheck);
        UserInfoEntity userInfoEntity = baseMapper.selectOne(queryWrapper);
        if (userInfoEntity == null){
            // 新用户
            UserInfoEntity user = new UserInfoEntity();
            user.setUserAccount(phoneCheck);
            user.setUserName(phoneCheck);
            this.save(user);

            UserInfoEntity entity = baseMapper.selectOne(queryWrapper);

            return entity;
        }else {

            return userInfoEntity;
        }
    }

    @Override
    public UserInfoEntity checkPassword(CheckPassWord checkPassWord) throws PasswordErrorException, UserNotExsitException, UserNotSetPasswordException{


        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", checkPassWord.getPhone());
        UserInfoEntity user = this.baseMapper.selectOne(queryWrapper);

        if (user == null){
            // 用户不存在
            throw new UserNotExsitException();
        }
        if (user.getPassword().equals("123456")){
            // 验证通过 返回user
            throw new UserNotSetPasswordException();
        }else if (
                // 找个解密工具
                // DigestUtils.md5DigestAsHex(password.getBytes())
                bCryptPasswordEncoder.matches(checkPassWord.getPassword(), user.getPassword())
        ){
            return user;
        }else {
            throw new PasswordErrorException();
        }
    }

    @Override
    public List<EquipmentInfoEntity> getEquipList(String userId) {

        if (userId != null) {
            List<EquipmentInfoEntity> equipList = equipmentInfoService.getByUserId(Integer.valueOf(userId));
            return equipList;
        }else {
            return null;
        }
    }

}